<template>
  <p style="font-size: 25px;font-weight: bold;margin-left: 700px;margin-top: 40px">保证金管理</p>
    <el-button @click="add" style="margin-top: 50px;">添加</el-button>
    <div> 
        <el-table :data="tableData" >
            <el-table-column prop="name" label="员工姓名"  />
            <el-table-column prop="price" label="余额" />
            <el-table-column  label="操作" >
                <template #default="scope">
                    <el-button @click="del(scope.row.id)">删除</el-button>
                    <el-button @click="editdata(scope.row)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
    <el-pagination
      v-model:current-page="query.page"
      v-model:page-size="query.size"
      :page-sizes="[2,3,4]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="count"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 添加弹窗 -->
    <el-dialog v-model="flag" title="保证金" width="500">
    <el-form :model="form">
      <el-form-item label="员工姓名" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="充值金额" :label-width="formLabelWidth">
        <el-input v-model="form.price" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="flag = false">取消</el-button>
        <el-button type="primary" @click="edit">
          充值
        </el-button>
      </div>
    </template>
  </el-dialog>
  

  <!-- 编辑弹窗 -->
  <el-dialog v-model="set" title="保证金" width="500">
    <el-form :model="bianji">
      <el-form-item label="充值金额" :label-width="formLabelWidth">
        <el-input v-model="bianji.price" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="addres">
          充值
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>


<script setup>
import { ref,onMounted } from "vue";
import axios from "axios";
let flag = ref(false);
let count=ref(0)
let tableData = ref([]);
//列表
const getlist = () => {
  axios.get(`http://127.0.0.1:8000/user/deposit/?page=${query.value.page}&size=${query.value.size}`)
  .then((res) => {
    console.log(res);
    tableData.value = res.data.results;
    count.value = res.data.count;
  });
}
let set = ref(false);
//编辑
let bianji = ref({
  price:"",
  id:"",
});
const addres=()=>{
  axios.put(`http://127.0.0.1:8000/user/deposit/${bianji.value.id}/`,bianji.value)
  .then((res) => {
    console.log(res);
    set.value = false;
    getlist();
  })
}
//删除
const del = (id) => {
  axios.delete(`http://127.0.0.1:8000/user/deposit/${id}/`)
  .then((res) => {
    console.log(res);
    getlist();
  });
}
//编辑
const editdata = (row) => {
  set.value = true;
  bianji.value = row;
}

onMounted(() => {
  getlist()
})
let form = ref({
    price:"",
    name:"",
})
let query=ref({
  page:1,
  size:2
})
const handleSizeChange=(num)=>{
  query.value.size=num
  getlist()
}
const handleCurrentChange=(num)=>{
  query.value.page=num
  getlist()
}
//添加
const edit = () => {
  axios.post("http://127.0.0.1:8000/user/deposit/",form.value)
  .then((res) => {
    console.log(res);
    getlist()
    flag.value = false;
  });
}
//添加弹窗
const add = () => {
  flag.value = true;
}
</script>


<style scoped> 

</style>
